<?php

namespace admin {

    use C\lib\Oss;
    use C\mod\bag;
    use C\mod\orderPiece;
    use C\mod\pieceState;
    use C\mod\user;
    use C\mod\worker;

    class control extends ctr {

        // --- 监控/订单 ---

        public function order() {

            global $_STATE_PIECE, $_STATE_IN_PIECE;

            if($worker = $this->getLogAds()) {

                $this->render('control/order', [
                    '_STATE_PIECE' => $_STATE_PIECE,
                    '_STATE_IN_PIECE' => $_STATE_IN_PIECE
                ]);
            }

        }

        // --- 订单 pull ---

        public function orderPull() {

            global $_PIECE_TYPE_PRODUCT, $_PIECE_TYPE_PAY, $_STATE_PIECE, $_STATE_IN_PIECE;

            if($worker = $this->getLogWorker()) {

                $jPieces = [];
                $jUsers = [
                    '0' => [
                        'phone' => '测试账户'
                    ]
                ];

                $pieces = orderPiece::getList([['state', 'in', ['0', '1']]], NULL, ['id', 'ASC']);
                $userIds = [];
                foreach ($pieces as $piece) {
                    $jPieces[] = [
                        'id' => $piece->id,
                        'address_sender' => $piece->address_sender,
                        'phone_sender' => $piece->phone_sender,
                        'name_sender' => $piece->name_sender,
                        'address_recipient' => $piece->address_recipient,
                        'phone_recipient' => $piece->phone_recipient,
                        'name_recipient' => $piece->name_recipient,
                        'time_add' => date('Y-m-d H:i:s', $piece->time_add),
                        'price_goods' => $piece->price_goods,
                        'price_pop' => $piece->price_pop,
                        'price' => $piece->price,
                        'type_pay' => $_PIECE_TYPE_PAY[$piece->type_pay],
                        'type_product' => $_PIECE_TYPE_PRODUCT[$piece->type_product],
                        'state_pay' => $piece->state_pay,
                        'state' => $piece->state,
                        'state_name' => $_STATE_PIECE[$piece->state],
                        'user_id' => $piece->user_id,
                        'state_in' => $piece->state_in,
                        'state_in_name' => $_STATE_IN_PIECE[$piece->state_in]
                    ];
                    $userIds[] = $piece->user_id;
                }
                $userIds = array_unique($userIds);
                $users = user::getList([['id', 'in', $userIds]]);
                foreach ($users as $user) {
                    $jUsers[$user->id] = [
                        'phone' => $user->phone
                    ];
                }
                $this->writeJson(1, [
                    'pieces' => $jPieces,
                    'users' => $jUsers
                ]);
            }

        }

        // --- 订单状态修改 ---

        public function orderState() {

            if($worker = $this->getLogWorker()) {
                if ($piece = orderPiece::get(['id' => $_POST['id']])) {
                    $piece->set([
                        'state' => $_POST['state'],
                        'state_in' => $_POST['stateIn']
                    ]);
                    $piece->update();
                    $pieceState = new pieceState();
                    $pieceState->set([
                        'worker_id' => '',
                        'piece_id' => $piece->id,
                        'state_in' => $piece->state_in,
                        'state' => $piece->state,
                        'user_show' => '0',
                        'time_add' => $_SERVER['REQUEST_TIME']
                    ]);
                    $this->writeJson(1);
                } else
                    $this->writeJson(0, '件不存在');
            }

        }

        // --- 监控认证用户认证 ---

        public function verify() {

            if($worker = $this->getLogAds()) {

                $this->render('control/verify');

            }

        }

        // --- 认证 pull ---

        public function verifyPull() {

            if($worker = $this->getLogAds()) {

                $jUsers = [];

                $users = user::getList([['realname', '!=', ''], ['verify', 'in', ['0', '2']]], NULL, ['id', 'ASC']);
                foreach ($users as $user) {
                    $jUsers[] = [
                        'id' => $user->id,
                        'phone' => $user->phone,
                        'realname' => $user->realname,
                        'idcard' => $user->idcard,
                        'time_add' => date('Y-m-d H:i:s', $user->time_add),
                        'idcard_oss_hand' => STATIC_PATH.$user->idcard_oss_hand,
                        'idcard_oss_front' => STATIC_PATH.$user->idcard_oss_front
                    ];
                }
                $this->writeJson(1, [
                    'users' => $jUsers
                ]);
            }

        }

        // --- 认证通过与驳回 ---

        public function verifyState() {

            if($worker = $this->getLogAds()) {

                if($user = user::get(['id' => $_POST['id'], ['verify', 'in', ['0', '2']]])) {

                    if($_POST['state'] == 'ok') {
                        $user->set('verify', '1');
                        $user->update();
                    } else {
                        Oss::connect();
                        Oss::deleteFile($user->idcard_oss_hand);
                        Oss::deleteFile($user->idcard_oss_front);
                        $user->set([
                            'realname' => '',
                            'idcard' => '',
                            'idcard_oss_hand' => '',
                            'idcard_oss_front' => ''
                        ]);
                        $user->update();
                    }
                    $this->writeJson(1);

                } else {
                    $this->writeJson(0, '用户不存在或者已经被认证');
                }

            }

        }

        // --- 包 ---

        public function bag() {

            if($worker = $this->getLogAds()) {

                $this->render('control/bag');

            }

        }
        public function bagPull() {

            global $_STATE_BAG;

            if($worker = $this->getLogAds()) {
                $jBags = [];
                $bags = bag::getList([['state', 'in', ['0', '1']]], NULL, ['id', 'ASC']);
                foreach ($bags as $bag) {
                    $jBags[] = [
                        'id' => $bag->id,
                        'user_id' => $bag->user_id,
                        'code' => $bag->code,
                        'commission' => $bag->commission,
                        'state' => $_STATE_BAG[$bag->state],
                        'time_add' => date('Y-m-d H:i:s', $bag->time_add)
                    ];
                }
                $this->writeJson(1, [
                    'bags' => $jBags
                ]);
            }

        }

        // --- 地图 ---

        public function map() {

            if($worker = $this->getLogAds()) {

                $this->render('control/map');

            }

        }

        public function mapPull() {

            if($worker = $this->getLogAds()) {

                $jWorkers = [];
                $workers = worker::getList([['time_update', '>=', $_SERVER['REQUEST_TIME'] - 3600]]);
                foreach ($workers as $worker) {
                    $jWorkers[] = [
                        'id' => $worker->id,
                        'phone' => $worker->phone,
                        'lat' => $worker->lat,
                        'lng' => $worker->lng,
                        'acc' => $worker->acc,
                        'dir' => $worker->dir,
                        'realname' => $worker->realname,
                        'time_update' => date('Y-m-d H:i:s', $worker->time_update),
                        'no' => $worker->no
                    ];
                }
                $this->writeJson(1, [
                    'time' => date('Y-m-d H:i:s'),
                    'workers' => $jWorkers
                ]);

            }

        }

    }

}

